package com.snpic.appaw.module.insure.controller.app.vehicleInformation;

import cn.hutool.core.lang.UUID;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.snpic.appaw.module.insure.controller.app.orc.vo.VehicleLicenseRespVO;
import com.snpic.appaw.module.insure.enums.InsureConstant;
import com.snpic.appaw.module.third.api.bjpt.dto.CarModelInfoInquiryReqDto;
import com.snpic.appaw.module.third.api.bjpt.dto.CarModelInfoInquiryReqTotalDto;
import com.snpic.appaw.module.third.api.bjpt.dto.CarModelInfoInquiryRespTotalDto;
import com.snpic.appaw.module.third.api.bjpt.dto.CxRequestHeadDto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.*;
import java.util.*;
import com.snpic.appaw.framework.common.pojo.PageResult;
import com.snpic.appaw.framework.common.pojo.CommonResult;
import com.snpic.appaw.framework.common.util.object.BeanUtils;
import static com.snpic.appaw.framework.common.pojo.CommonResult.success;
import com.snpic.appaw.module.insure.controller.app.vehicleInformation.vo.*;
import com.snpic.appaw.module.insure.dal.dataobject.vehicleInformation.VehicleInformationDO;
import com.snpic.appaw.module.insure.service.vehicleInformation.VehicleInformationService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "C端 - 首页车辆卡片/编辑家庭档案 - 车辆信息")
@RestController
@RequestMapping("/insure/vehicle-information")
@Validated
public class AppVehicleInformationController {

	@Resource
	private VehicleInformationService vehicleInformationService;

	@PostMapping("/create")
	@Operation(summary = "创建新车辆信息")
	/**
	 * 调用这个接口时，ECustomer必然是实名的。
	 */
	public CommonResult<Long> createVehicleInformation(@Valid @RequestBody AppVehicleInformationSaveReqVO createReqVO) {

		return success(vehicleInformationService.createVehicleInformation(createReqVO));
	}

	@PostMapping(value = "/update", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	@Operation(summary = "更新车辆信息")
	public CommonResult<VehicleLicenseRespVO> updateVehicleInformation(
		@RequestPart(value = "vehicleLicenseFile", required = false) @Schema(description = "上传的行驶证正面图片") MultipartFile vehicleLicenseFile,
		@RequestParam("data") String jsonData) throws JsonProcessingException {

		// 将 JSON 字符串反序列化为对象
		AppVehicleInformationSaveReqVO updateReqVO = new ObjectMapper().readValue(jsonData, AppVehicleInformationSaveReqVO.class);
		VehicleLicenseRespVO vehicleLicenseRespVO = vehicleInformationService.updateVehicleInformation(vehicleLicenseFile, updateReqVO);
		return success(vehicleLicenseRespVO);

	}

	@GetMapping("/delete")
	@Operation(summary = " 删除车辆信息")
	@Parameter(name = "id", description = "编号", required = true)
	public CommonResult<Boolean> deleteVehicleInformation(@RequestParam("id") Long id) {
		vehicleInformationService.deleteVehicleInformation(id);
		return success(true);
	}

	@Operation(summary = "获取车险用省城车牌分级列表")
	@PostMapping("/regionslist")
	@Cacheable(value = InsureConstant.CACHE_NAME_VEHICLE_INFORMATION,
		key = "'regionslist'", unless = "#result == null")
	public CommonResult<ReginsListRespVO> getReginsList() {
		return success(vehicleInformationService.getReginsList());
	}


	//    @GetMapping("/get")
//    @Operation(summary = "首页车辆卡片/编辑家庭档案 - 获取车辆信息")
//    @Parameter(name = "id", description = "编号", required = true)
	public CommonResult<AppVehicleInformationRespVO> getVehicleInformation(@RequestParam("id") Long id) {
		VehicleInformationDO vehicleInformation = vehicleInformationService.getVehicleInformation(id);
		return success(BeanUtils.toBean(vehicleInformation, AppVehicleInformationRespVO.class));
	}

	//    @DeleteMapping("/delete-list")
//    @Parameter(name = "ids", description = "编号", required = true)
//    @Operation(summary = "批量删除首页车辆卡片/编辑家庭档案 - 车辆信息")
	public CommonResult<Boolean> deleteVehicleInformationList(@RequestParam("ids") List<Long> ids) {
		vehicleInformationService.deleteVehicleInformationListByIds(ids);
		return success(true);
	}

	//    @GetMapping("/page")
//    @Operation(summary = "获得首页车辆卡片/编辑家庭档案 - 车辆信息分页")
	public CommonResult<PageResult<AppVehicleInformationRespVO>> getVehicleInformationPage(@Valid AppVehicleInformationPageReqVO pageReqVO) {
		PageResult<VehicleInformationDO> pageResult = vehicleInformationService.getVehicleInformationPage(pageReqVO);
		return success(BeanUtils.toBean(pageResult, AppVehicleInformationRespVO.class));
	}

	@GetMapping("/list")
	@Operation(summary = "获得车辆信息列表")
	public CommonResult<List<AppVehicleInformationRespVO>> getVehicleInformationList(@Valid AppVehicleInformationPageReqVO pageReqVO) {
		List<VehicleInformationDO> pageResult = vehicleInformationService.getVehicleInformationList(pageReqVO);
		return success(BeanUtils.toBean(pageResult, AppVehicleInformationRespVO.class));
	}

	@PostMapping("/swtichSelectStatus")
	@Operation(summary = "切换车辆的两个选中状态,")
	public CommonResult<Boolean> switchSelectStatus(@Valid @RequestBody SwitchSelectStatusReqVO switchSelectStatusReqVO) {
		boolean result = vehicleInformationService.switchSelectStatus(switchSelectStatusReqVO);
		return success(result);
	}


		/**
	 * 车辆车型查询接口
	 */
	@PostMapping("/carModelInfoInquiry")
	@Operation(summary = "车辆车型查询接口,前端专用")
	public CommonResult<CarModelInfoInquiryRespTotalDto> carModelInfoInquiry(
		@RequestParam @NotEmpty(message = "车牌号不能为空") String plateNo,
		@RequestBody @Valid CarModelInfoInquiryReqTotalDto reqDTO
	) {
		CarModelInfoInquiryRespTotalDto respTotalDto = vehicleInformationService.carModelInfoInquiry(plateNo,reqDTO);
		return success(respTotalDto);

	}



}